package net.tngou.tpage.action.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;

import net.tngou.tpage.action.BaseAction;
import net.tngou.tpage.jdbc.PageUtil;
import net.tngou.tpage.pojo.Access;
import net.tngou.tpage.pojo.Element;
import net.tngou.tpage.pojo.Result;
import net.tngou.tpage.pojo.Tempdata;
import net.tngou.tpage.pojo.Template;
import net.tngou.tpage.service.TemplateService;
import net.tngou.tpage.util.Pinyin4j;
import net.tngou.tpage.util.StringUtil;

/**
 * 模板
 * @author tngou@tngou.net
 *
 */
public class TemplateApi extends BaseAction{

	
	/**
	 * 共有模块
	 */
	@Override
	public void execute() throws ServletException, IOException {
		if(user==null){
			run_nologin();return;
		}
		TemplateService service = new TemplateService();
		String[] tags = request.getParameterValues("tags");
		if(tags!=null&&tags.length>0)
		{			
			PageUtil page = service.getPage(ask.getPage(), ask.getRows(), 0, tags, ask.getKeyword());
			printJson(toJsonPage(page));
			return;
		}
		String keyword = ask.getKeyword();
		Map<String, Object> map = new HashMap<>();
		if(StringUtil.isNotEmpty(keyword))
		{
			map.put("id", keyword);
			map.put("name", keyword);
			map.put("pinyin", keyword+"%");
		}
		PageUtil page = service.getPage(ask.getPage(), ask.getRows(),"user=0",map,Template.class);
		printJson(toJsonPage(page));	
		
	}
	
	/**
	 * 私有模板
	 */
	public void my() {
		if(user==null){
			run_nologin();return;
		}
		TemplateService service = new TemplateService();
		String[] tags = request.getParameterValues("tags");
		if(tags!=null&&tags.length>0)
		{			
			PageUtil page = service.getPage(ask.getPage(), ask.getRows(), user.getId(), tags, ask.getKeyword());
			printJson(toJsonPage(page));
			return;
		}
		String keyword = ask.getKeyword();
		Map<String, Object> map = new HashMap<>();
		if(StringUtil.isNotEmpty(keyword))
		{
			map.put("id", keyword);
			map.put("name", keyword);
			map.put("pinyin", keyword+"%");
		}
		PageUtil page = service.getPage(ask.getPage(), ask.getRows(),"user="+user.getId(),map,Template.class);
		printJson(toJsonPage(page));	
		
	}
	
	/**
	 * 添加基本信息
	 */
	public void add() {
		if(user==null){
			run_nologin();return;
		}
		Template bean = getAsk(Template.class);
		if(isEmpty(bean.getName()))
		{
			run_false("请求的参数不正确！");return;	
		}
		if(user.getRole()==0)
		{
			bean.setUser(user.getId());
		}else
		{
			bean.setUser(0);//设置为公共数据
		}
		bean.setPinyin(Pinyin4j.ToHanyuPinyinHead(bean.getName(), true));//设置拼音
		TemplateService service = new TemplateService();
		long b = service.save(bean);
		if(b>0){run_success("添加成功！",b);return;
		}else {run_false("添加失败！");return;}
		
	}
	
	/**
	 * 更新 基本信息
	 */
	public void update() {
		if(user==null){
			run_nologin();return;
		}
		Template bean = getAsk(Template.class);
		if(ask.getId()==0)
		{
			run_false("请求的参数不正确！");return;	
		}
		Template template = bean.get(ask.getId());
		if(template==null)
		{
			run_false("请求的数据不正确！");return;	
		}
		if(!((template.getUser()==0&&user.getRole()==0)||template.getUser()==user.getId())){
			run_false("没有修改权限！");return;	
		}
		TemplateService service = new TemplateService();
		long b = service.update(bean, template);
		if(b>0){run_success("添加成功！");return;
		}else {run_false("添加失败！");return;}
		
	}
	
	
	/**
	 * 更新HTML
	 */
	public void updatehtml() {
		if(user==null){
			run_nologin();return;
		}
		Template bean = getAsk(Template.class);
		if(ask.getId()==0||isEmpty(bean.getHtml()))
		{
			run_false("请求的参数不正确！");return;	
		}
		Template template = bean.get(ask.getId());
		if(template==null)
		{
			run_false("请求的数据不正确！");return;	
		}
		if(!((template.getUser()==0&&user.getRole()==0)||template.getUser()==user.getId())){
			run_false("没有修改权限！");return;	
		}
		template.setHtml(bean.getHtml());
		long b = template.update();
		if(b>0){run_success("添加成功！",b);return;
		}else {run_false("添加失败！");return;}
				
	}

	/**
	 * 删除
	 */
	public void delete() {
		if(user==null){
			run_nologin();return;
		}
		Template bean = getAsk(Template.class);
		if(ask.getId()==0)
		{
			run_false("请求的参数不正确！");return;	
		}
		Template template = bean.get(ask.getId());
		if(template==null)
		{
			run_false("请求的数据不正确！");return;	
		}
		if(!((template.getUser()==0&&user.getRole()==0)||template.getUser()==user.getId())){
			run_false("没有删除权限！");return;	
		}
		int total = new Access().totalCount("template="+template.getId())+new Result().totalCount("template="+template.getId());
		if(total>0)
	    {
	    	run_false("当前已有绑定数据,不能进行删除！");return;	
	    }
		boolean b = template.delete();
		if(b){	
			new Tempdata().delete("template="+ask.getId());
			run_success("删除成功！");
		}else {run_false("删除失败！");return;}
	}
	
	/**
	 * 拷贝
	 */
	public void copy() {
		if(user==null){
			run_nologin();return;
		}
		Template bean = getAsk(Template.class);
		if(ask.getId()==0)
		{
			run_false("请求的参数不正确！");return;	
		}
		Template template = bean.get(ask.getId());
		if(template==null)
		{
			run_false("请求的数据不正确！");return;	
		}
		long userid=0;
		if(user.getRole()!=0)userid=user.getId();
		TemplateService service = new TemplateService();		
		long b = service.copy(template,userid);
		if(b>0){run_success("拷贝成功！");return;
		}else {run_false("拷贝失败！");return;}
	}
}
